package cn.edu.scau.cmi.chenruilin.hibernate;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;

import java.util.*;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;


public class SpringController {
	
	@FXML
    private Button show_stu;
	
	@FXML
    private Button show_tea;
	
	@FXML
    private Label s_output;

    @FXML
    private TextField s_id_in;

    @FXML
    private Label t_output;

    @FXML
    private Button del_stu;

    @FXML
    private Button alt_stu;

    @FXML
    private TextField tutor_in;

    @FXML
    private Button alt_tea;

    @FXML
    private Button alt_tutor;

    @FXML
    private Button add_stu;

    @FXML
    private TextField t_name_in;

    @FXML
    private TextField t_id_in;

    @FXML
    private Button del_tea;

    @FXML
    private Button choose_tutor;

    @FXML
    private TextField s_name_in;

    @FXML
    private Button add_tea;
    
    
    String getAllStudent() {
    	String out = String.format("%-12s %-12s %-12s\n", "ID",	"name", "tutor");
    	Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
    	//StudentDao studentDao = new StudentDao();
    	ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
		
    	List students = studentDao.findAll();
		Iterator<?> studentIterator = students.iterator();
		while(studentIterator.hasNext()){
			Student student = (Student) studentIterator.next();
			if(student.getTeacher()==null) {
				out = out + String.format("%-12d %-12s %-12s\n", student.getId(), student.getName() , student.getTeacher());
				//out = out + student.getId()+"		" + student.getName() + "		" + student.getTeacher() + "\n";
			}
			else {
				out = out + String.format("%-12d %-12s %-12d\n", student.getId(), student.getName() , student.getTeacher().getId());
				//out = out + student.getId().toString() + "		" + student.getName() + "		" + student.getTeacher().getId() + "\n";
			}
		}
		transaction.commit();
		session.close();
		return out;
    }
    
    String getAllTeacher() {
    	String out = String.format("%-12s %-12s\n", "ID",	"name");
    	Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		//TeacherDao teacherDao = new TeacherDao();
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
		
    	List teachers = teacherDao.findAll();
		Iterator<?> teacherIterator = teachers.iterator();
		while(teacherIterator.hasNext()){
			Teacher teacher = (Teacher) teacherIterator.next();
			out = out + String.format("%-12d %-12s\n", teacher.getId(), teacher.getName());
		}
		transaction.commit();
		session.close();
		return out;
    }
    
    String operateStudent(int flag) {
    	Long s_id = Long.parseLong(s_id_in.getText());
    	String s_name = s_name_in.getText();
    	String out = null;
    	//Long t_id = Long.parseLong(tutor_in.getText());
    	Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
    	Student s = new Student();
    	s.setId(s_id);
    	s.setName(s_name);
    	
    	//StudentDao studentDao = new StudentDao();
    	ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
		
		
    	if(flag == 0) studentDao.save(s);
    	else if(flag == 1) studentDao.delete(s);
    	else if(flag == 2) studentDao.update(s);

    	transaction.commit();
		session.close();
		out = getAllStudent();
		return out;
    }
    
    String operateTeacher(int flag) {
    	Long t_id = Long.parseLong(t_id_in.getText());
    	String t_name = t_name_in.getText();
    	String out = null;
    	Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
    	Teacher t = new Teacher();
    	t.setId(t_id);
    	t.setName(t_name);
    	
    	//TeacherDao teacherDao = new TeacherDao();
    	ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		TeacherDao teacherDao=(TeacherDao) ac.getBean("teacherDao");
		
    	if(flag == 0) teacherDao.save(t);
    	else if(flag == 1) teacherDao.delete(t);
    	else if(flag == 2) teacherDao.update(t);

    	transaction.commit();
		session.close();
		out = getAllTeacher();
		return out;
    }
    
    @FXML
    void addStudent() {
    	try {
    		String out = operateStudent(0);
    		s_output.setText(out);
    	}
    	catch(Exception e) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("该学生ID已存在！");
    		al.showAndWait();
    	}	
    }

    @FXML
    void delStudent() {
    	try {
    		String out = operateStudent(1);
    		s_output.setText(out);
    	}
    	catch(Exception e) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("该学生不存在！");
    		al.showAndWait();
    	}
    }

    @FXML
    void altStudent() {
    	try {
    		String out = operateStudent(2);
    		s_output.setText(out);
    	}
    	catch(Exception e) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("该学生ID不存在！");
    		al.showAndWait();
    	}
    }

    @FXML
    void chooseTutor() {
    	try {
    		Long s_id = Long.parseLong(s_id_in.getText());
        	String s_name = s_name_in.getText();
        	String out = null;
        	Long t_id = Long.parseLong(tutor_in.getText());
        	Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
    		Transaction transaction = session.beginTransaction();
    		
        	Student s = new Student();
        	s.setId(s_id);
        	s.setName(s_name);
        	Teacher t = new Teacher();
        	t.setId(t_id);
        	s.setTeacher(t);
        	
        	//StudentDao studentDao = new StudentDao();
        	ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
    		StudentDao studentDao=(StudentDao) ac.getBean("studentDao");
        	
        	studentDao.update(s);

        	transaction.commit();
    		session.close();
    		out = getAllStudent();
    		s_output.setText(out);
    	}
    	catch(Exception e) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("请确保该老师ID存在于Teacher表中！");
    		al.showAndWait();
    	}
    	
    }

    @FXML
    void altTutor() {
    	try {
    		chooseTutor();
    	}
    	catch(Exception e) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("请确保该老师ID存在于Teacher表中！");
    		al.showAndWait();
    	}
    }

    @FXML
    void addTeacher() {
    	try {
    		String out = operateTeacher(0);
    		t_output.setText(out);
    	}
    	catch(Exception e) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("该老师ID已存在！");
    		al.showAndWait();
    	}
    }

    @FXML
    void altTeacher() {
    	try {
    		String out = operateTeacher(2);
    		t_output.setText(out);
    	}
    	catch(Exception e) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("该老师ID不存在！");
    		al.showAndWait();
    	}
    }

    @FXML
    void delTeacher() {
    	try {
    		String out = operateTeacher(1);
    		t_output.setText(out);
    	}
    	catch(Exception e) {
    		Alert al = new Alert(AlertType.WARNING);
    		al.setTitle("WARNING");
    		al.setHeaderText(null);
    		al.setContentText("该老师ID不存在！");
    		al.showAndWait();
    	}
    }
    
    @FXML
    void showStudent() {
    	String out = getAllStudent();
    	s_output.setText(out);
    }

    @FXML
    void showTeacher() {
    	String out = getAllTeacher();
    	t_output.setText(out);
    }
}
